/*
Class Name      : GeneralUtilWCM
Created by      : Suneetha Allam Venkata
Created On      : 08/02/2013
Description     : This class  for   General functions 
Version         : 1.0                 

*/

global without sharing class GeneralUtilWCM {
    
    webService static String convertLead(String objectId){
    	
    	List<Lead> lstLead = [SELECT FirstName,LastName,Personal_Email__c,State,Program__r.name,Anticipated_Start_Date__c,Lead_id__C,Home_Phone__c FROM Lead where id=:objectId];
    	
    	if(lstLead.size() > 0){
    		
    		DataFormatter formatter = new DataFormatter();
    		String FirstName=lstLead[0].FirstName==null?'':lstLead[0].FirstName;
	        String LastName=lstLead[0].LastName==null?'':lstLead[0].LastName;
	        String EmailAddress=lstLead[0].Personal_Email__c==null?'':lstLead[0].Personal_Email__c;
	        String State=lstLead[0].State==null?'':lstLead[0].State;
	        String ProgramOfInterest=lstLead[0].Program__r.name==null?'':lstLead[0].Program__r.name;
	        String StartTerm=lstLead[0].Anticipated_Start_Date__c==null?'':lstLead[0].Anticipated_Start_Date__c.month()+'/'+lstLead[0].Anticipated_Start_Date__c.year();
	        String SalesforceID = lstLead[0].Lead_id__C==null?'':lstLead[0].Lead_id__C;
	        String Phone=lstLead[0].Home_Phone__c==null?'':formatter.phoneFormatter(lstLead[0].Home_Phone__c);
    		
    		String strConvertResult = QuickAppRequest_WS.convertLeadOrUpdateAccount(FirstName,LastName,EmailAddress,State,ProgramOfInterest,StartTerm,'Application Started','None',SalesforceID,Phone);
    		
    		if(strConvertResult.contains('Success:')){
    			Account convertedAccount = [select id from Account where Personal_Email__pc=:lstLead[0].Personal_Email__c LIMIT 1];
    			return 'Success: '+convertedAccount.id;
    		}else{
    			return strConvertResult;
    		}
    	}else{
    		return 'Failure: Lead not available';
    	}
    }
    
    webService static String getCaseEditCloseAbilityStatus(String objectId){
        
        try{
            UserRecordAccess objUserAccess = [SELECT RecordId,HasEditAccess FROM UserRecordAccess
                                            WHERE UserId = :userInfo.getUserId()
                                            AND RecordId = :objectId];
            if(objUserAccess.HasEditAccess){
                return 'CAN_EDIT';
            }else{
                return 'CANNOT_EDIT';
            }   
        }Catch(Exception e){
            return e.getMessage();
        }
        
        return 'CANNOT_EDIT';
    }
    
    webService static String getNewCaseFromLeadPageLayoutURL(String leadId){
    	String returnURL='';
    	try{
    		Lead objLead = [Select Name from Lead where id=:leadId][0];
    		
    		west_case_management_utils__c objUtils = [Select Value__c from west_case_management_utils__c where name='LeadFieldInCaseObject'][0];
    		
    		returnURL = 'CF'+objUtils.Value__c+'='+EncodingUtil.urlEncode(objLead.Name, 'UTF-8')+'&CF'+objUtils.Value__c+'_lkid='+leadId;
    	}Catch(Exception e){
    		return e.getMessage();
    	}
    	
    	return returnURL;
    }
    
    public static void createErrorLog(String strClassName,String strInput,String strErrorMessage,Boolean boolSendEmail){
    	Error_Log__c objErrorLog = new Error_Log__c();
    	
    	objErrorLog.Class_Name__c = strClassName;
    	objErrorLog.Input_Parameters__c = strInput;
    	objErrorLog.Error_Message__c = strErrorMessage;
    	objErrorLog.Send_Email__c = boolSendEmail;
    	
    	insert objErrorLog;
    	
    }
    
    public static void Warning(string message)
        {
        
         ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.WARNING, message ));
        }
    
    
    
    public static void ErrorMessage(string message)
        {
        
         ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR,message ));
        
      
        }
    public static Id getRecordType()
    {
      return Schema.Sobjecttype.Case.getRecordTypeInfosByName().get('West - Support').getRecordTypeId();
    
    }
    public Static String getUserProfileName()
    {
        String  UserProfileName='';
        try
        {
            UserProfileName=[select name from profile where id=:UserInfo.getProfileId() limit 1].name;
        }
        Catch(Exception e)
        {
            //errMessage('Exception =====>'+e);
            //GeneralUtil_AC.saveApplicationLog(null, null,null, null, e.getMessage(), 'ConfirmAndCloseCaseForSSA_AC','','');
        }
          
        return UserProfileName;
    }
    
    //To Get List of Queues Not ADA & ODM
    
        public Static Set<ID>  getNotADAODMQueueIdsList()
    {
        Set<Id> setAssignedQueueIDs = new Set<Id>();
        
        for(Group objGroup: [SELECT Id, Name, Type FROM Group WHERE id in (SELECT GroupId FROM GroupMember WHERE userorgroupid = :userInfo.getUserId()) AND Type = 'Queue']){
            setAssignedQueueIDs.add(objGroup.Id);
        }
        Set<Id> setQueueIds = new Set<Id>();
        try
        {
          List<QueueSobject> lstQueueSobject=new List<QueueSobject>();
          lstQueueSobject = [Select Id, QueueId,Queue.Name, SobjectType 
                            from QueueSobject 
                            where Queue.Name Like '%CM - %' 
                            and sobjectType = 'Case'
                            and queueId NOT IN :setAssignedQueueIDs];
          for(QueueSobject objQueueSObject:lstQueueSObject){
                   setQueueIds.add(objQueueSObject.queueid);
           }
           
            
        }
        Catch(Exception e)
        {
            //errMessage('Exception =====>'+e);
            //GeneralUtil_AC.saveApplicationLog(null, null,null, null, e.getMessage(), 'ConfirmAndCloseCaseForSSA_AC','','');
        }
                  
        return   setQueueIds;
    }
    
	public static void initiateLiteratureReconcilationSchedule(){
        // Cron Expression which will execute everyday at 4:00 A.M MST.
        //String cronExpression = '0 0 4 ? * *';   
        String cronExpression = '0 0 * ? * *';
        System.schedule('Case Escalator-Age Calculator', cronExpression, new CaseEscalatorScheduler() );
    }
        
    public Static Task gettask()
    {
                  //  TaskObj.whatId = userInfo.getUserId();
                   // TaskObj.whoId = userInfo.getUserId();
                   Task TaskObj=New Task();
                    TaskObj.Subject='Call';
                    TaskObj.Type = 'Phone';
                    TaskObj.Next_Call__c=dateTime.now();    
                    TaskObj.Status='Completed';
                    TaskObj.Priority='High';
                    TaskObj.Description='test';
                    TaskObj.OwnerId=userInfo.getUserId();
                    insert TaskObj;
                    return TaskObj;
    
        
    }
    
    //To Get List of Queues Not ADA & ODM
    
        public Static Set<ID>  getQueueIdNotADAorODM()
    {
        Set<Id> setAssignedQueueIDs = new Set<Id>();
        
        for(Group objGroup: [SELECT Id, Name, Type FROM Group WHERE id in (SELECT GroupId FROM GroupMember WHERE userorgroupid = :userInfo.getUserId()) AND Type = 'Queue']){
            setAssignedQueueIDs.add(objGroup.Id);
        }
     Set<Id> setQueueIds = new Set<Id>();
        try
        {
          List<QueueSobject> lstQueueSobject=new List<QueueSobject>();
          lstQueueSobject = [Select Id, QueueId,Queue.Name, SobjectType 
                                from QueueSobject where Queue.Name Like '%CM - %' 
                                and sobjectType = 'Case'
                                and queueid IN :setAssignedQueueIDs];
                                
          for(QueueSobject objQueueSObject:lstQueueSObject){
                   setQueueIds.add(objQueueSObject.queueid);
           }
           
            
        }
        Catch(Exception e)
        {
            //errMessage('Exception =====>'+e);
            //GeneralUtil_AC.saveApplicationLog(null, null,null, null, e.getMessage(), 'ConfirmAndCloseCaseForSSA_AC','','');
        }
                  
        return   setQueueIds;
    }
    
     public Static void saveLog(String ErrorMessage, String MethodName, String  ClassName)
     {
        
     }
}